package com.PMSys.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.PMSys.Demain.Staff;
import com.PMSys.Service.StaffService;

public class StaffServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	StaffService staffService = new StaffService();
	/**
	 * Constructor of the object.
	 */
	public StaffServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		
		//����request�е�����
		String StaffList=request.getParameter("StaffList");
		String selectBasicStaffState=request.getParameter("selectBasicStaffState");
		
		if(StaffList!=null && StaffList!=""){
			doSelectALLStaff(request, response);
		}
		
		if(selectBasicStaffState!=null && selectBasicStaffState!=""){
			doSelectBasicStaff(request, response);
		}
	}
	
	
	public void doSelectBasicStaff(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		//��ȡ��ѯ����
		String s_id = request.getParameter("s_id");
		
		Staff staff = new Staff();
		
		staff.setS_id(s_id);
		staff = staffService.getSelectIdStaff(staff);
		
		if(staff == null)
		{
			response.getWriter().print("<script>alert('ϵͳ��������!');location.href='"+request.getContextPath()+"/Staff/InformationManagerStaff/StaffList.jsp'</script>");
		}
		else
		{
			//���ݴ�ŵ�request��
			request.setAttribute("Staff", staff);
			
			//��ת��ָ��ҳ��
			request.getRequestDispatcher("/.jsp").forward(request, response);
		}	
	}
	
	
	public void doSelectALLStaff(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		List<Staff> allStaff = new ArrayList<Staff>();
		
		allStaff = staffService.getAllStaff();
		
		if(allStaff.size() == 0)
		{
			response.getWriter().print("<script>alert('ϵͳ��������!');location.href='"+request.getContextPath()+"/Staff/InformationManagerStaff/StaffList.jsp'</script>");
		}
		else
		{
			//���ݴ�ŵ�request��
			request.setAttribute("allStaff", allStaff);
			
			//��ת��ָ��ҳ��
			request.getRequestDispatcher("/Staff/InformationManagerStaff/StaffList.jsp").forward(request, response);
		}	
		
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
